# 数据库工具类
import peewee
import peewee_async

import datetime


# 创建数据库链接对象

database = peewee_async.PooledMySQLDatabase("scoial",host="127.0.0.1",port=3306,user="root",password="zjcnb")

class Base_Model(peewee.Model):
    id = peewee.IntegerField(primary_key=True, unique=True, constraints=[peewee.SQL('AUTO_INCREMENT')])
    create_time = peewee.DateTimeField(default=datetime.datetime.now(),help_text='入库时间')

    def save(self,*args,**kwargs):
        if self._pk is None:   # 判断什么时候赋值入库方法
            # 赋值
            self.create_time = datetime.datetime.now()

        return super(Base_Model,self).save(*args,**kwargs)
    class Meta:
        # 传递数据库连接
        database = database # 第十行的database


# 用户表
class User_Model(Base_Model):
    # id = peewee.IntegerField(primary_key=True,unique=True,constraints=[peewee.SQL('AUTO_INCREMENT')])
    name = peewee.CharField(null=False,max_length=50)
    email = peewee.CharField(null=False,max_length=100,unique=True)
    password = peewee.CharField(null=False,max_length=100)
    state = peewee.IntegerField(null=False,default=0)
    avtar = peewee.CharField(null=True,max_length=500,default='https://img.51miz.com/Element/00/88/08/86/716b81c7_E880886_bebe0ef3.png')
    cate = peewee.IntegerField(null=False, default=0) # 平台类型0  gitee 1  微博 2

    class Meta:
        db_table = 'user_tb'  # 指定表明
# 古诗词表
# class Poetry_Model(Base_Model):
#     title = peewee.CharField(null=False,max_length=100)
#     dynasty = peewee.CharField(null=False,max_length=100)
#     name = peewee.CharField(null=False,max_length=20)
#     text = peewee.TextField()
#
#     class Meta:
#         db_table = 'poetry_tb'  # 指定表明

# 表情包表
# class Image_Model(Base_Model):
#     title = peewee.CharField(null=False,max_length=100)
#     path = peewee.CharField(null=False,max_length=500)
#
#
#     class Meta:
#         db_table = 'image_tb'  # 指定表明
# 计数器表
class CheckNum_Model(Base_Model):  # 计数器表
    email = peewee.CharField(null=False,unique=True,max_length=100)
    num = peewee.IntegerField(null=False,default=0)
    class Meta:
        db_table = 'check_num_tb'  # 指定表明
# 节点表
class Node_Model(Base_Model):
    name = peewee.CharField(null=False,unique=True,max_length=20)
    method = peewee.CharField(null=False,unique=True,max_length=20)
    class Meta:
        db_table = 'node_tb'
# 权限表
class Access_Model(Base_Model):
    uid = peewee.IntegerField(null=False)
    nid = peewee.IntegerField(null=False)
    class Meta:
        db_table = 'access_tb'
# 角色表
class Role_Model(Base_Model):
    name = peewee.CharField(null=False,unique=True,max_length=20)
    class Meta:
        db_table = 'role_tb'
# 用户与角色
class User_Role_Model(Base_Model):
    uid = peewee.IntegerField(null=False)
    rid = peewee.IntegerField(null=False)
    class Meta:
        db_table = 'user_role_tb'
# 权限表  角色与节点
class Access_role_Model(Base_Model):
    rid = peewee.IntegerField(null=False)
    nid = peewee.IntegerField(null=False)
    class Meta:
        db_table = 'access_role_tb'

# 话题表
class Talk_Model(Base_Model):
    name = peewee.CharField(null=False,unique=True,max_length=50)
    class Meta:
        db_table = 'talk_tb'
# 帖子表
class Sub_Model(Base_Model):
    content = peewee.TextField(null=False)
    imgs = peewee.CharField(null=True,max_length=300)
    user = peewee.ForeignKeyField(User_Model)
    clock = peewee.DateTimeField(null=True)
    # 0所有人可见 1粉丝可见 2好友 3自己可见
    state = peewee.IntegerField(null=False,default=0)
    class Meta:
        db_table = 'Sub_tb'
# 帖子与话题
class Sub_Talk_Model(Base_Model):
    tid = peewee.IntegerField(null=False)
    sid = peewee.IntegerField(null=False)










class Usermodel(Base_Model):
    name = peewee.CharField(max_length=20,unique=True,null=False)
    phone = peewee.CharField(max_length=11,unique=True,null=False)
    usercode = peewee.CharField(max_length=50,unique=True,null=False)
    password = peewee.CharField(max_length=50,null=False)
    class Meta:
        db_table = 'ks_user_tb'

class Productmodel(Base_Model):
    name = peewee.CharField(max_length=100,unique=True,null=False)
    price = peewee.CharField(max_length=20)
    vip_price = peewee.CharField(max_length=20)
    img = peewee.CharField(max_length=500)
    class Meta:
        db_table = 'ks_product_tb'

class Ordermodel(Base_Model):
    order_id = peewee.CharField(max_length=80, unique=True, null=False)
    goods = peewee.ForeignKeyField(Productmodel)
    users = peewee.ForeignKeyField(Usermodel)
    price = peewee.CharField(max_length=80,null=False)
    class Meta:
        db_table = 'ks_order_tb'




if __name__ == '__main__':
    # 创建表
    # pass
    Usermodel.create_table(True)
    Productmodel.create_table(True)
    Ordermodel.create_table(True)
    # Access_role_Model.create_table(True)
    # Sub_Model.drop_table(True)
    # User_Model.drop_table(True)   # 删除表
    # user = User_Model.create(email='123@qq.com',password='111')
    # CheckNum_Model.create(email='123',num=1)
    # print(user.id)/






